package com.wenya.counter.ui.add;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.wenya.counter.R;
import com.wenya.counter.data.bean.ScriptureBean;
import com.wenya.counter.data.db.CounterDatabase;
import com.wenya.counter.databinding.AddActivityBinding;

import java.util.List;
import java.util.Objects;

public class AddActivity extends AppCompatActivity {
    private AddActivityBinding addActivityBinding;
    private List<ScriptureBean> oldList;
    private ScriptureBean tagBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addActivityBinding = DataBindingUtil.setContentView(this, R.layout.add_activity);
        initView();
        initData();
    }

    private void initView() {
        addActivityBinding.nameEdita.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String name = addActivityBinding.nameEdita.getText().toString();
                if (name == null || name.trim().length() == 0 || oldList == null || oldList.size() == 0) {
                    return;
                }
                name = name.trim();
                tagBean = null;
                for (ScriptureBean scriptureBean : oldList) {
                    if (Objects.equals(scriptureBean.getName(), name)) {
                        tagBean = scriptureBean;
                        addActivityBinding.divisorEdita.setText(String.valueOf(scriptureBean.getDivisor()));
                        addActivityBinding.timesEdita.setText(String.valueOf(scriptureBean.getTimes()));
                        break;
                    }
                }
            }
        });
        addActivityBinding.btCancel.setOnClickListener(view -> finish());
        addActivityBinding.btAdd.setOnClickListener(view -> {
            if (tagBean != null) {
                AlertDialog alertDialog = new AlertDialog.Builder(AddActivity.this)
                        .setTitle(R.string.tips)
                        .setMessage(R.string.addOldTips)
                        .setPositiveButton(R.string.ok, (dialog, which) -> {
                            save();
                        })
                        .setNegativeButton(R.string.cancel, null)
                        .create();
                alertDialog.show();
            } else {
                save();
            }
        });
    }

    private void save() {
        String name = addActivityBinding.nameEdita.getText().toString();
        if (name == null || name.trim().length() == 0) {
            Toast.makeText(this, R.string.nameInputTips, Toast.LENGTH_SHORT).show();
            return;
        }
        String divisor = addActivityBinding.divisorEdita.getText().toString();
        if (divisor == null || divisor.trim().length() == 0) {
            Toast.makeText(this, R.string.divisorInputTips, Toast.LENGTH_SHORT).show();
            return;
        }
        int divisorNumber;
        try {
            divisorNumber = Integer.parseInt(divisor);
        } catch (Exception e) {
            Toast.makeText(this, R.string.divisorInputErrorTips, Toast.LENGTH_SHORT).show();
            return;
        }
        if (divisorNumber <= 0) {
            Toast.makeText(this, R.string.divisorInputErrorTips2, Toast.LENGTH_SHORT).show();
            return;
        }

        String times = addActivityBinding.timesEdita.getText().toString();
        if (times == null || times.trim().length() == 0) {
            Toast.makeText(this, R.string.timesInputTips, Toast.LENGTH_SHORT).show();
            return;
        }
        int timesNumber;
        try {
            timesNumber = Integer.parseInt(times);
        } catch (Exception e) {
            Toast.makeText(this, R.string.timesInputErrorTips, Toast.LENGTH_SHORT).show();
            return;
        }
        if (timesNumber < 0) {
            Toast.makeText(this, R.string.timesInputErrorTips2, Toast.LENGTH_SHORT).show();
            return;
        }

        ScriptureBean scriptureBean = new ScriptureBean();
        scriptureBean.setName(name.trim());
        scriptureBean.setDivisor(divisorNumber);
        scriptureBean.setTimes(timesNumber);
        CounterDatabase.getInstance().basicDataDao().insert(scriptureBean);
        Toast.makeText(this, R.string.addSuc, Toast.LENGTH_SHORT).show();
        finish();
        setResult(1008);
    }

    private void initData() {
        oldList = CounterDatabase.getInstance().basicDataDao().getAll();
    }
}
